package com.core.zyyz.amc.modules.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.core.common.utils.StringUtils;
import com.core.common.utils.uuid.UUID;
import com.core.zyyz.amc.modules.domain.AmcCusBaseInfoB;
import com.core.zyyz.amc.modules.domain.AmcNccBankdoc;
import com.core.zyyz.amc.modules.service.AmcCusBaseInfoBService;
import com.core.zyyz.amc.modules.service.AmcNccBankaccbasService;
import com.core.zyyz.amc.modules.service.AmcNccBankdocService;
import com.core.zyyz.util.CodeMakerUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.domain.AmcCusBankB;
import com.core.zyyz.amc.modules.service.AmcCusBankBService;
import com.core.common.core.page.TableDataInfo;

/**
 * 客户银行账户Controller
 *
 * @author lfs
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/amc/cusbank")
public class AmcCusBankBController extends BaseController
{
    @Autowired
    private AmcCusBankBService amcCusBankBService;
    @Autowired
    private AmcNccBankdocService amcNccBankdocService;
    @Autowired
    private AmcCusBaseInfoBService amcCusBaseInfoBService;
    @Autowired
    private AmcNccBankaccbasService amcNccBankaccbasService;

    /**
     * 查询客户银行账户列表
     */
    @PreAuthorize("@ss.hasPermi('amc:cusbank:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcCusBankB amcCusBankB)
    {
        startPage();
        List<AmcCusBankB> list = amcCusBankBService.selectAmcCusBankBList(amcCusBankB);
        return getDataTable(list);
    }


    /**
     * 获取客户银行账户详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:cusbank:query')")
    @GetMapping(value = "/{cusBankId}")
    public AjaxResult getInfo(@PathVariable("cusBankId") String cusBankId)
    {
        return success(amcCusBankBService.selectAmcCusBankBByCusBankId(cusBankId));
    }

    /**
     * 新增客户银行账户
     */
    @PreAuthorize("@ss.hasPermi('amc:cusbank:add')")
    @Log(title = "客户银行账户", businessType = BusinessType.INSERT)
    @PostMapping("/insertCusbank")
    @Transactional
    public AjaxResult add(@RequestBody AmcCusBankB amcCusBankB) {
        amcCusBankB.setCusBankId(UUID.randomUUID().toString());
        CodeMakerUtil cm = new CodeMakerUtil();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        amcCusBankB.setCusBankNumber(cm.createCode("CUS", df.format(new Date()),3));//生成客户银行账户表编号
        //保存开户行信息
        if(StringUtils.isNotEmpty(amcCusBankB.getPkBankdoc())){
            AmcNccBankdoc amcNccBankdoc = amcNccBankdocService.queryNccBankDoc(amcCusBankB.getPkBankdoc());
            if(amcNccBankdoc!=null){
                amcCusBankB.setBankName(amcNccBankdoc.getName());
                amcCusBankB.setPkBanktype(amcNccBankdoc.getPkBanktype());
            }
        }
        // 判断银行账号是否存在 如果存在则不保存 并且提示
        List<AmcCusBankB> bankBList = amcCusBankBService.queryAmcCusBankBList(amcCusBankB.getBankAccountNumber());
        if(bankBList != null && bankBList.size()>0){
            return error("该银行账号已存在，关联客户编号为:"+bankBList.get(0).getCustomerNumber());
        }

        int count = amcCusBankBService.insertAmcCusBankB(amcCusBankB);

        try{
            //新增客户上的新增银行账户，先不调接口，在保存客户时调用
            if(amcCusBankB.getCustomerNumber() != null){
                AmcCusBaseInfoB amcCusBaseInfoB = amcCusBaseInfoBService.selectByCustomerNumber(amcCusBankB.getCustomerNumber());
                if (amcCusBankB != null){
                    amcCusBankB.setCustomerName(amcCusBaseInfoB.getCustomerName());
                    amcCusBankB.setBusinessCode(amcCusBaseInfoB.getCustomerId());
                    amcCusBankBService.updateAmcCusBankB(amcCusBankB);
                    if(amcCusBaseInfoB.getPkCustomer() != null) {
                        AmcCusBankB entity = amcCusBankBService.selectAmcCusBankBByCusBankId(amcCusBankB.getCusBankId());
                        if (entity.getPkBankaccbas() != null) {
                            /** 接口还未写完 */
                            String msg =  amcNccBankaccbasService.updateNccBankaccbasByCusBank(amcCusBankB.getCusBankId());
                            if(StringUtils.isNotEmpty(msg) && !"ok".equals(msg)){
                                // 删除
//                                amcCusBankBService.removeById(amcCusBankB.getCusBankId());
                                // 给出提示
//                                return resultError(msg);
                            }
                        }else {

                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("NCC客户银行账户新增失败",e);
        }
        return toAjax(count);
    }

    /**
     * 修改客户银行账户
     */
    @PreAuthorize("@ss.hasPermi('amc:cusbank:edit')")
    @Log(title = "客户银行账户", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCusbank")
    @Transactional
    public AjaxResult edit(@RequestBody AmcCusBankB amcCusBankB)
    {
        return toAjax(amcCusBankBService.updateAmcCusBankB(amcCusBankB));
    }

    /**
     * 删除客户银行账户
     */
    @PreAuthorize("@ss.hasPermi('amc:cusbank:remove')")
    @Log(title = "客户银行账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cusBankIds}")
    public AjaxResult remove(@PathVariable String[] cusBankIds)
    {
        return toAjax(amcCusBankBService.deleteAmcCusBankBByCusBankIds(cusBankIds));
    }
}
